package com.mxx.tomcat.verification;

import com.mxx.common.conv.JacksonConverter;
import com.mxx.common.utils.CollectionUtil;
import com.mxx.tomcat.ResponseWriter;
import com.mxx.tomcat.verification.annotation.Verify;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Mr. Zeng
 * @Date: 2021/4/8 19:29
 * @Description: 暂无描述
 */
public abstract class AbstractFormUrlValidateArgumentResolver extends AbstractValidateArgumentResolver {

    public AbstractFormUrlValidateArgumentResolver(List<HttpMessageConverter<?>> converters, JacksonConverter jacksonConverter, ResponseWriter responseWriter) {
        super(converters, jacksonConverter, responseWriter);
    }


    public Map<String, String> getRequestParameterMap(NativeWebRequest nativeWebRequest, MethodParameter methodParameter, Verify.Type type) {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        String storageKey = String.format("%s_Map", type.name());
        Map<String, String> requestParameter = (Map<String, String>) request.getAttribute(storageKey);
        if (requestParameter == null) {
            String queryString = request.getQueryString();
            if (queryString == null) {
                queryString = "";
            }
            String[] texts = queryString.split("&");
            String[] urlNames = new String[texts.length];
            for (int i = 0; i < urlNames.length; i++) {
                String str = texts[i];
                int spitIndex = str.indexOf("=");
                if (spitIndex == -1) {
                    continue;
                }
                urlNames[i] = str.substring(0, spitIndex);
            }
            Map<String, String> urlParameter = new HashMap<>();
            for (String urlName : urlNames) {
                String urlVal = request.getParameter(urlName);
                urlParameter.put(urlName, urlVal);
            }
            request.setAttribute(String.format("%s_Map", Verify.Type.URL.name()), urlParameter);

            Enumeration<String> enumeration = request.getParameterNames();
            Map<String, String> formParameter = new HashMap<>();
            while (enumeration.hasMoreElements()) {
                String name = enumeration.nextElement();
                String val;
                if (CollectionUtil.exists(urlNames, name)) {
                    String[] values = request.getParameterValues(name);
                    if (values.length > urlNames.length) {
                        val = values[values.length - 1];
                    } else {
                        continue;
                    }
                } else {
                    val = request.getParameter(name);
                }
                if (val != null){
                    formParameter.put(name, val);
                }
            }
            request.setAttribute(String.format("%s_Map", Verify.Type.FORM.name()), formParameter);
        }
        requestParameter = (Map<String, String>) request.getAttribute(storageKey);
        return requestParameter;
    }

}
